//
//  DHS2_SearchView_Products.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/15.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_SearchView_Products.h"
#import "DHBrandProductCollectionCell.h"
#import "DHS2_ProductDetailController.h"

@interface DHS2_SearchView_Products()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (assign, nonatomic) NSInteger pageIndex;
@property (strong, nonatomic) NSMutableArray *productsArr;

@end

@implementation DHS2_SearchView_Products

+ (DHS2_SearchView_Products *)shareDHS2_SearchView_Products{
    DHS2_SearchView_Products *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DHS2_SearchView_Products class]) owner:self options:nil] objectAtIndex:0];
    return view;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"DHBrandProductCollectionCell" bundle:nil] forCellWithReuseIdentifier:kDHBrandProductCollectionCell];
    self.collectionView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(searchData)];
    self.collectionView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(searchMoreData)];
}

- (NSMutableArray *)productsArr{
    if (!_productsArr) {
        _productsArr = [NSMutableArray array];
    }
    return _productsArr;
}

//搜索
- (void)searchData{
    self.pageIndex = 0;
    [self.collectionView.mj_footer resetNoMoreData];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@"商品" forKey:@"type"];
    [params setObject:self.keyWord forKey:@"key"];
    [params setObject:@(self.pageIndex) forKey:@"pageIndex"];
    [params setObject:@(20) forKey:@"pageSize"];
    [[DHNetAPIClient shareDHNetAPIClient] request_app_searchWithParams:params AndBlock:^(id data, NSError *error) {
        if (data) {
            [self.collectionView.mj_header endRefreshing];
            
            [self.productsArr removeAllObjects];
            
            NSDictionary *dic = [data objectForKey:@"results"];
            [self.productsArr addObjectsFromArray:[dic objectForKey:@"products"]];
            
            if (self.productsArr.count == 0) {
                [self.collectionView.mj_footer endRefreshingWithNoMoreData];
            }
            
            [self.collectionView reloadData];
        }
    }];
}

- (void)searchMoreData{
    [self.collectionView.mj_footer endRefreshing];
}

#pragma mark - UICollectionViewDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.productsArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHBrandProductCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDHBrandProductCollectionCell forIndexPath:indexPath];
    [cell uploadCell:self.productsArr[indexPath.row]];
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return (CGSize){(kScreenWidth - 16 - 15) / 3,200};
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.productsArr objectAtIndex:indexPath.row];
    DHS2_ProductDetailController *vc = [[DHS2_ProductDetailController alloc] initWithProduct:model];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

@end
